<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 6/10/21
 * Time: 1:16 AM
 */

namespace app\api\controller\v1;


use app\api\model\ProductOrder;
use app\api\model\ProductOrderContent;
use app\api\model\StoreGroup as StoreGroupModel;
use app\api\model\Store as StoreModel;
use app\api\model\StoreUser as StoreUserModel;
use app\api\model\StoreUserItem;
use app\api\model\UserInvitation;
use app\api\service\Token as TokenService;
use app\api\model\User as UserModel;
use app\api\model\Product;


class StoreOwnerRank
{
    // 店铺销售额统计
    public function getStoreOwnerRankList()
    {
        $uid = TokenService::getCurrentUid();
        $storeUser = StoreUserModel::where('user_id',$uid)
            ->with('user')->find();
        if($storeUser['type'] == 4) {
            $storeUserItem = StoreUserItem::where('store_user_id',$storeUser['id'])->select()->toArray();
            $storeIds = array_column($storeUserItem,'store_id');
            $storeGroups = StoreGroupModel::where('store_id','in',$storeIds)
                ->select()->toArray();
            $storeUsers = StoreUserModel::where('store_id','in',$storeUser['store_id'])
                ->select()->toArray();
            array_push($storeUsers, $storeUser);
        } else {
            $storeGroups = StoreGroupModel::where('store_id',$storeUser['store_id'])
                ->select()->toArray();
            $storeUsers = StoreUserModel::where('store_id',$storeUser['store_id'])
                ->select()->toArray();
        }

        $store_group = [];
        foreach ($storeGroups as $k=>$v){
            $storeUsers = StoreUserModel::where('group_id',$v['id'])
                ->select()->toArray();
            $storeUserIds = array_column($storeUsers,'id');
            $userInvitation = UserInvitation::where('type','eq',2)
                ->where('inviter','in',$storeUserIds)
                ->select()->toArray();
            $ids = array_column($userInvitation,'invitee');
            $price = ProductOrder::where('user_id','in',$ids)
                ->where('status','gt',1)
                ->sum('actual_price');
            $storeGroups[$k]['price'] = $price;
            array_push($store_group,$storeGroups[$k]);
        }
        if(count($store_group) > 0) {
            $store_group = arraySort($store_group,'price');
        }

        $array = [];
        foreach ($storeUsers as $k=>$v){
            $userInvitation = UserInvitation::where('type','eq',2)
                ->where('inviter','eq',$v['id'])
                ->select()->toArray();
            $ids = array_column($userInvitation,'invitee');
            $price = ProductOrder::where('user_id','in',$ids)
                ->where('status','gt',1)
                ->sum('actual_price');
            $storeUsers[$k]['price'] = $price;
            if($price > 0){
                array_push($array,$storeUsers[$k]);
            }
        }
        if(count($array) > 0) {
            $array = arraySort($array,'price');
        }
        return [
            'store_group' => $store_group,
            'store_user' => $array
        ];
    }

    // 新客数排行
    public function getStoreOwnerRankUser()
    {
        $uid = TokenService::getCurrentUid();
        $storeUser = StoreUserModel::where('user_id',$uid)
            ->with('user')->find();
        $new_user = [];
        $whereFour = [];
        $userInviteOne = UserInvitation::where('type',2)->select()->toArray();
        $idsOne = array_column($userInviteOne,'invitee');
        $whereFour[] = ['id','in',$idsOne];
        $whereFour[] = ['status','eq',3];
        $whereFour[] = ['new_type','eq',1];
        if($storeUser['type'] == 4) {
            $storeUserItem = StoreUserItem::where('store_user_id',$storeUser['id'])->select()->toArray();
            $storeIds = array_column($storeUserItem,'store_id');
            $whereFour[] = ['store_id','in',$storeIds];
        } else {
            $whereFour[] = ['store_id','eq',$storeUser['store_id']];
        }
        $userOne = UserModel::where($whereFour)->select()->toArray();
        foreach ($userOne as $ka=>$va){
            $whereFive = [];
            $whereFive[] = ['user_id','eq',$userOne[$ka]['id']];
            $whereFive[] = ['status', 'gt', 1];
            $order = ProductOrder::where($whereFive)->find();
            if($order) {
                array_push($new_user,$userOne[$ka]);
            }
        }
        return $new_user;
    }

    // 产品销量
    public function getStoreOwnerRankProduct()
    {
        $uid = TokenService::getCurrentUid();
        $storeUser = StoreUserModel::where('user_id',$uid)
            ->with('user')->find();
        if($storeUser['type'] == 4) {
            $storeUserItem = StoreUserItem::where('store_user_id',$storeUser['id'])->select()->toArray();
            $storeIds = array_column($storeUserItem,'store_id');
            $product = Product::where('store_id','in',$storeIds)
                ->select()->toArray();
        } else {
            $product = Product::where('store_id',$storeUser['store_id'])
                ->select()->toArray();
        }
        foreach ($product as $k=>$v){
            $product[$k]['count'] = ProductOrderContent::where('product_id','eq',$v['id'])
                ->where('status','gt',1)->sum('product_number');
        }
        if(count($product) > 0) {
            $product = arraySort($product,'count');
        }
        return $product;
    }

    // 待提现金额
    public function getStoreOwnerRankMoney()
    {
        $uid = TokenService::getCurrentUid();
        $storeUser = StoreUserModel::where('user_id',$uid)
            ->with('user')->find();
        if($storeUser['type'] == 4) {
            $storeUserItem = StoreUserItem::where('store_user_id',$storeUser['id'])->select()->toArray();
            $storeIds = array_column($storeUserItem,'store_id');
            $storeUsers = StoreUserModel::where('store_id','in',$storeIds)
                ->with('user')
                ->select()->each(function($item){
                    $item['money'] = $item['user']['money'];
                })->toArray();
        }else {
            $storeUsers = StoreUserModel::where('store_id',$storeUser['store_id'])
                ->with('user')
                ->select()->each(function($item){
                    $item['money'] = $item['user']['money'];
                })->toArray();
        }

        if(count($storeUsers) > 0) {
            $storeUsers = arraySort($storeUsers,'money');
        }
        return $storeUsers;
    }
}